<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2002 Bharat Mediratta
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

class AlbumItem {
    var $image;
    var $thumbnail;
    var $caption;
    var $hidden;
    var $highlight;
    var $highlightImage;
    var $isAlbumName;
    var $clicks;
    var $keywords;
    var $comments; // array of comment objects
    var $uploadDate; // date the item was uploaded
    var $itemCaptureDate; // associative array of date the item was captured    
    // not in EPOCH so we can support dates < 1970
    var $exifData;

    function setUploadDate( $uploadDate = "" ) // upload date should only be set at file upload time.
    {
        global $gallery;

        if ( $uploadDate ) { // set the upload time from the time provided
            $this->uploadDate = $uploadDate;
        } else { // if nothing is passed in, get the upload time from the file creation time
            $dir = $gallery->album->getAlbumDir();
            $name = $this->image->name;
            $tag = $this->image->type;
            $file = "$dir/$name.$tag";
            $this->uploadDate = filectime( $file );
        } 
    } 

    function getUploadDate()
    {
        if ( !$this->uploadDate ) {
            return 0;
        } else {
            return $this->uploadDate;
        } 
    } 

    function setItemCaptureDate( $itemCaptureDate = "" )
    {
        global $gallery;
        /*$itemCaptureDate should be passed in as an associative array with the following elements:
	 	$itemCaptureDate["hours"]
		$itemCaptureDate["minutes"]
		$itemCaptureDate["seconds"]
		$itemCaptureDate["mon"]
		$itemCaptureDate["mday"]
		$itemCaptureDate["year"]
		*/
        if ( !$itemCaptureDate ) {
            // we want to attempt to set the $itemCaptureDate from the information that
            // is available to us.  First, look in the exif data if it is a jpeg file.  If that
            // doesn't help us, then use the file creation date.
            $dir = $gallery->album->getAlbumDir();
            $name = $this->image->name;
            $tag = $this->image->type;
            $file = "$dir/$name.$tag";
            $itemCaptureDate = getItemCaptureDate( $file );
        } 

        $this->itemCaptureDate = $itemCaptureDate;
    } 

    function getItemCaptureDate()
    { 
        // need to set this value for old photos that don't yet contain it.
        if ( !$this->itemCaptureDate ) {
            return 0;
        } else {
            return $this->itemCaptureDate;
        } 
    } 

    function getExif( $dir, $forceRefresh = 0 )
    {
        global $gallery;
        $file = $dir . "/" . $this->image->name . "." . $this->image->type;

        /*
		 * If we don't already have the exif data, get it now.
		 * Otherwise return what we have.
		 */
        $needToSave = 0;
        if ( !empty( $this->exifData ) && !$forceRefresh ) {
            $status = 0;
        } else {
            list( $status, $exifData ) = getExif( $file );
            if ( $status == 0 ) {
                $this->exifData = $exifData;
                if ( !strcmp( $gallery->app->cacheExif, _YES ) ) {
                    $needToSave = 1;
                } else {
                    $needToSave = 0;
                } 
            } 
        } 
        return array( $status, $this->exifData, $needToSave );
    } 

    function numComments()
    {
        return sizeof( $this->comments );
    } 

    function getComment( $commentIndex )
    {
        return $this->comments[$commentIndex-1];
    } 

    function integrityCheck( $dir )
    {
        $changed = 0;

        if ( $this->image ) {
            if ( $this->image->integrityCheck( $dir ) ) {
                $changed = 1;
            } 

            if ( $this->thumbnail ) {
                if ( $this->thumbnail->integrityCheck( $dir ) ) {
                    $changed = 1;
                } 
            } 

            if ( $this->highlight && $this->highlightImage ) {
                if ( $this->highlightImage->integrityCheck( $dir ) ) {
                    $changed = 1;
                } 
            } 
        } 
        return $changed;
    } 

    function addComment( $comment, $IPNumber, $name )
    {
        global $gallery;

        if ( $gallery->user ) {
            $UID = $gallery->user->getUID();
        } else {
            $UID = "";
        } 

        $comment = new Comment( $comment, $IPNumber, $name, $UID );

        $this->comments[] = $comment;

        return 0;
    } 

    function deleteComment( $comment_index )
    {
        array_splice( $this->comments, $comment_index-1, 1 );
    } 

    function setKeyWords( $kw )
    {
        $this->keywords = $kw;
    } 

    function getKeyWords()
    {
        return $this->keywords;
    } 

    function resetItemClicks()
    {
        $this->clicks = 0;
    } 

    function getItemClicks()
    {
        if ( !isset( $this->clicks ) ) {
            $this->resetItemClicks();
        } 
        return $this->clicks;
    } 

    function incrementItemClicks()
    {
        if ( !isset( $this->clicks ) ) {
            $this->resetItemClicks();
        } 
        $this->clicks++;
    } 

    function hide()
    {
        $this->hidden = 1;
    } 

    function unhide()
    {
        $this->hidden = 0;
    } 

    function isHidden()
    {
        return $this->hidden;
    } 

	// mostly taken from gallery 1.4.x and changed to work with this version
	function setHighlight($dir, $bool) {
		global $gallery;
		
		$this->highlight = $bool;
		
		/*
		 * if it is now the highlight make sure it has a highlight
         	 * thumb otherwise get rid of it's thumb (ouch!).
		 */

		if ($this->highlight) {
			if (!isset($name)) {
				$name = $this->image->name;
				$tag = $this->image->type;
				$srcdir = $dir;
				$srcitem = $this;
				if ($this->isAlbum()) {
					$name = $this->getAlbumName();
					$nestedAlbum = new Album();
					$nestedAlbum->load($name);
					list ($srcalbum, $srcitem) = $nestedAlbum->getHighlightedItem();
					if ($srcalbum !== null && $srcitem !== null) {
						$srcdir = $srcalbum->getAlbumDir();
						$tag = $srcitem->image->type;
					}
					else {
						if (is_object($this->highlightImage)) {
							$this->highlightImage->simpleDelete($dir);
							$this->highlightImage = null;
							echo("oops 1 <br>");
						}
						return;
					}
				}
			}
			$size = $gallery->app->highlight_size;

			if ($srcitem->image->thumb_width > 0  && !$this->isMovie()) {
				// Crop it first
				$ret = cut_image("$srcdir/".$srcitem->image->name.".$tag",
						"$dir/$name.tmp.$tag",
						$srcitem->image->thumb_x,
						$srcitem->image->thumb_y,
						$srcitem->image->thumb_width,
						$srcitem->image->thumb_height);

				// Then resize it down
				if ($ret) {
					$ret = resize_image("$dir/$name.tmp.$tag", 
							    "$dir/$name.highlight.$tag",
							    $size);
				}
				fs_unlink("$dir/$name.tmp.$tag");
			} elseif ($this->isMovie()) {
				create_thumb_movie( "$dir/$name.$tag", "$dir/$name.tmp.jpg" );
					// rename the file $tag to follow local convension
                    			$tag = 'jpg';
                    			$ret = resize_image( "$dir/$name.tmp.$tag", "$dir/$name.highlight.$tag", $size );
					// get rid of tmp file
                    			fs_unlink( "$dir/$name.tmp.$tag" ); 
			} else {
				$ret = resize_image("$srcdir/".$srcitem->image->name.".$tag",
						    "$dir/$name.highlight.$tag",
						    $size);
			}

			if ($ret) {
				list($w, $h) = getDimensions("$dir/$name.highlight.$tag");

				$high = new Image;
				$high->setFile($dir, "$name.highlight", "$tag");
				$high->setDimensions($w, $h);
				$this->highlightImage = $high;

			}
		} else {
			if (is_object($this->highlightImage)) {
				$this->highlightImage->simpleDelete($dir);
				$this->highlightImage = null;
			}
		}
	}
	
	
	
    function isHighlight()
    {
        return $this->highlight;
    } 

    function getThumbDimensions( $size = 0 )
    {
        if ( $this->thumbnail ) {
            return $this->thumbnail->getDimensions( $size );
        } else {
            return array( 0, 0 );
        } 
    } 

    function getDimensions()
    {
        if ( $this->image ) {
            return $this->image->getDimensions();
        } else {
            return array( 0, 0 );
        } 
    } 

    function isResized()
    {
        $im = $this->image;
        return $im->isResized();
    } 

    function rotate( $dir, $direction, $thumb_size )
    {
        global $gallery;

        $name = $this->image->name;
        $type = $this->image->type;
        $retval = rotate_image( "$dir/$name.$type", "$dir/$name.$type", $direction );
        if ( !$retval ) {
            return $retval;
        } 
        list( $w, $h ) = getDimensions( "$dir/$name.$type" );
        $this->image->setRawDimensions( $w, $h );

        if ( $this->isResized() ) {
            rotate_image( "$dir/$name.sized.$type", "$dir/$name.sized.$type", $direction );
            list( $w, $h ) = getDimensions( "$dir/$name.sized.$type" );
            $this->image->setDimensions( $w, $h );
        } 

        /* Reset the thumbnail to the default before regenerating thumb */
        $this->image->setThumbRectangle( 0, 0, 0, 0 );
        $this->makeThumbnail( $dir, $thumb_size );
    } 

    function setPhoto( $dir, $name, $tag, $thumb_size, $pathToThumb = "" )
    {
        global $gallery;

        /*
	 	 * Sanity: make sure we can handle the file first.
		 */
        if ( !isMovie( $tag ) && !valid_image( "$dir/$name.$tag" ) ) {
            return _XG_INVALID_IMAGE . $name . $tag;
        } 

        /* Set our image. */
        $this->image = new Image;
        $this->image->setFile( $dir, $name, $tag );

        $ret = $this->makeThumbnail( $dir, $thumb_size, $pathToThumb );
        return $ret;
    } 

    function makeThumbnail( $dir, $thumb_size, $pathToThumb = "" )
    {
        global $gallery;
        $name = $this->image->name;
        $tag = $this->image->type;
        if ( isMovie( $tag ) ) {
            /* If ffmpeg is available generate thumbnail. else. Use a preset thumbnail */
            $need_image = 1;
            if ( $gallery->app->use_ffmpeg == _YES ) {
                if ( create_thumb_movie( "$dir/$name.$tag", "$dir/$name.thumb.jpg" ) == 0 ) {
                    $ret = resize_image( "$dir/$name.thumb.jpg", "$dir/$name.thumb.jpg", $thumb_size );
                    $need_image = ( $ret == 0 );
                } elseif ( isdebugging() ) {
                    print _XG_WRARFFM;
                } 
            } 
            if ( $need_image ) {
                fs_copy( $gallery->app->movieThumbnail, "$dir/$name.thumb.jpg" );
            } 
            unset( $need_image );
            $this->thumbnail = new Image;
            $this->thumbnail->setFile( $dir, "$name.thumb", "jpg" );
            list( $w, $h ) = getDimensions( "$dir/$name.thumb.jpg" );
            $this->thumbnail->setDimensions( $w, $h );
        } else {
            /* Make thumbnail (first crop it spec) */
            if ( $pathToThumb ) {
                $ret = copy ( $pathToThumb, "$dir/$name.thumb.$tag" );
            } else if ( $this->image->thumb_width > 0 ) {
                $ret = cut_image( "$dir/$name.$tag",
                    "$dir/$name.thumb.$tag",
                    $this->image->thumb_x,
                    $this->image->thumb_y,
                    $this->image->thumb_width,
                    $this->image->thumb_height );
                if ( $ret ) {
                    $ret = resize_image( "$dir/$name.thumb.$tag",
                        "$dir/$name.thumb.$tag", $thumb_size );
                } 
            } else {
                $ret = resize_image( "$dir/$name.$tag", "$dir/$name.thumb.$tag",
                    $thumb_size );
            } 

            if ( $ret ) {
                $this->thumbnail = new Image;
                $this->thumbnail->setFile( $dir, "$name.thumb", $tag );

                list( $w, $h ) = getDimensions( "$dir/$name.thumb.$tag" );
                $this->thumbnail->setDimensions( $w, $h );

                /* if this is the highlight, remake it */
                if ( $this->highlight ) {
                    $this->setHighlight( $dir, 1 );
                } 
            } else {
                return _XG_UNABLE_TO_MAKE_THUMBNAIL . " (" . $ret . ")";
            } 
        } 

        return 0;
    } 

    function getThumbnailTag( $dir, $size = 0, $attrs = "" )
    {
        if ( $this->thumbnail ) {
            return $this->thumbnail->getTag( $dir, 0, $size, $attrs );
        } else {
            return "<i>" . _XG_NO_THUMBNAIL . "</i>";
        } 
    } 

    function getHighlightTag( $dir, $size = 0, $attrs )
    {
        if ( is_object( $this->highlightImage ) ) {
            return $this->highlightImage->getTag( $dir, 0, $size, $attrs );
        } else {
            return "<i>" . _XG_NO_HIGHLIGHT . "</i>";
        } 
    } 

    function getPhotoTag( $dir, $full = 0 )
    {
        if ( $this->image ) {
            return $this->image->getTag( $dir, $full );
        } else {
            return "about:blank";
        } 
    } 

    function getPhotoPath( $dir, $full = 0 )
    {
        if ( $this->image ) {
            return $this->image->getPath( $dir, $full );
        } else {
            return "about:blank";
        } 
    } 

    function getPhotoId( $dir )
    {
        if ( $this->image ) {
            return $this->image->getId( $dir );
        } else {
            return _XG_UNKOWN;
        } 
    } 

    function delete( $dir )
    {
        if ( $this->image ) {
            $this->image->delete( $dir );
        } 

        if ( $this->thumbnail ) {
            $this->thumbnail->delete( $dir );
        } 
    } 
	
	function isAlbum() {
		return ($this->isAlbumName !== NULL) ? true : false;
	}
    
	function setCaption( $cap )
    {
        $this->caption = $cap;
    } 

    function getCaption()
    {
        return $this->caption;
    } 

    function setIsAlbumName( $name )
    {
        $this->isAlbumName = $name;
    } 

    function getIsAlbumName()
    {
        return $this->isAlbumName;
    } 

    function isMovie()
    {
        return isMovie( $this->image->type );
    } 

    function resize( $dir, $target, $pathToResized = "" )
    {
        if ( $this->image ) {
            $this->image->resize( $dir, $target, $pathToResized );
        } 
    } 
} 

?>
